package ar.com.cooker.services.impl;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import ar.com.cooker.services.IngredientServices;
import ar.com.cooker.services.pojo.IngredientPojo;
import ar.com.cooker.services.uow.ingredient.GetAllIngredientUOW;
import ar.com.cooker.services.uow.ingredient.SaveAndUpdateIngredientUOW;
import ar.com.cooker.services.util.ManagerBean;


@Path("/ingredientSercives")
@Component(value = "ingredientSerciveBean")
public class IngredientSercivesImpl implements IngredientServices {

	ManagerBean managerBean;
	
	@Autowired
	private GetAllIngredientUOW getAllIngredientUOW;
	
	@Autowired
	private SaveAndUpdateIngredientUOW saveAndUpdateIngredientUOW; 
	
	@POST
	@Path("/post/createIngredient/{callback}")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response createIngredient(IngredientPojo ingredient) {
		if(getSaveAndUpdateIngredientUOW()==null){
			init();
		}
		return Response.ok().build();
	}

	@DELETE
	@Path("/deleteImgredient/{idIngredient}/{callback}")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response deleteIngredient(IngredientPojo idIngredient) {
		return Response.ok().build();
	}

	@GET
	@Path("/get/getAllIngredient/{callback}")
	@Produces(MediaType.APPLICATION_JSON)
	public IngredientPojo[] getAllIngredient() {
		if(getGetAllIngredientUOW() == null){
			init();
		}
		return getGetAllIngredientUOW().execute();
	}

	@POST
	@Path("/post/updateIngredient/{callback}")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response updateIngredient(IngredientPojo ingredient) {
		if(getSaveAndUpdateIngredientUOW()==null){
			init();
		}
		return Response.ok().build();
	}

	public void setGetAllIngredientUOW(GetAllIngredientUOW getAllIngredientUOW) {
		this.getAllIngredientUOW = getAllIngredientUOW;
	}

	public GetAllIngredientUOW getGetAllIngredientUOW() {
		return getAllIngredientUOW;
	}

	public void setSaveAndUpdateIngredientUOW(SaveAndUpdateIngredientUOW saveAndUpdateIngredientUOW) {
		this.saveAndUpdateIngredientUOW = saveAndUpdateIngredientUOW;
	}

	public SaveAndUpdateIngredientUOW getSaveAndUpdateIngredientUOW() {
		return saveAndUpdateIngredientUOW;
	}
	
	private void init(){
		if(managerBean == null){
			managerBean = ManagerBean.getManager();
			setGetAllIngredientUOW((GetAllIngredientUOW) managerBean.getBean("getAllIngredientUOW"));
			setSaveAndUpdateIngredientUOW((SaveAndUpdateIngredientUOW) managerBean.getBean("saveAndUpdateIngredientUOW"));			
		}
	}

}
